package com.jkoss.wine.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.common.vo.ZtreeBean;
import com.jkoss.wine.system.entity.Commonly;
import com.jkoss.wine.system.entity.Permission;
import com.jkoss.wine.system.service.ICommonlyService;
import com.jkoss.wine.system.service.IPermissionService;
import com.jkoss.wine.system.service.IRolePermissionService;

/**
 * 常用功能 前端控制器
 * 
 * @Author 周华军
 * @Version 1.0, 2018-11-01
 * @See
 * @Since com.jkoss.wine.system.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/system/commonly")
public class CommonlyController extends BaseController {

	@Autowired
	private ICommonlyService iCommonlyService;
	@Autowired
	private IRolePermissionService iRolePermissionService;
	@Autowired
	private IPermissionService iPermissionService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/system/commonly/list")
	public String list(DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Page resultPage = iCommonlyService.selectPage(dwzPageBean.toPage(), null);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "system/commonly/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/system/commonly/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "system/commonly/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/system/commonly/toInsert")
	@ResponseBody
	public Object insert(@Valid Commonly commonly, HttpServletRequest request, HttpServletResponse response) {
		if (iCommonlyService.insert(commonly)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/system/commonly/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iCommonlyService.selectById(id));
		return "system/commonly/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/system/commonly/toUpdate")
	@ResponseBody
	public Object update(@Valid Commonly commonly, HttpServletRequest request, HttpServletResponse response) {
		if (iCommonlyService.updateById(commonly)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/system/commonly/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iCommonlyService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
